package com.ruoyi.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.SysCommoditySetting;
import com.ruoyi.system.mapper.SysCommoditySettingMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysCommodityLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysCommodityOrderMapper;
import com.ruoyi.system.domain.SysCommodityOrder;
import com.ruoyi.system.service.ISysCommodityOrderService;

/**
 * 订单管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-19
 */
@Service
public class SysCommodityOrderServiceImpl implements ISysCommodityOrderService {
    @Autowired
    private SysCommodityOrderMapper sysCommodityOrderMapper;
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    SysCommoditySettingMapper sysCommoditySettingMapper;

    @Autowired
    ISysCommodityLogService sysCommodityLogService;
    /**
     * 查询订单管理
     *
     * @param id 订单管理主键
     * @return 订单管理
     */
    @Override
    public SysCommodityOrder selectSysCommodityOrderById(Long id) {
        return sysCommodityOrderMapper.selectSysCommodityOrderById(id);
    }

    /**
     * 查询订单管理列表
     *
     * @param sysCommodityOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<SysCommodityOrder> selectSysCommodityOrderList(SysCommodityOrder sysCommodityOrder) {
        List<SysCommodityOrder> sysCommodityOrders = sysCommodityOrderMapper.selectSysCommodityOrderList(sysCommodityOrder);
        for (SysCommodityOrder commodityOrder : sysCommodityOrders) {
            SysUser sysUser = sysUserMapper.selectUserById(commodityOrder.getUserId());
            commodityOrder.setOrderPlacer(sysUser.getUserName());
            SysCommoditySetting commoditySetting = sysCommoditySettingMapper.selectSettingById(commodityOrder.getOrderSettingId() );
            Date createTime = commodityOrder.getCreateTime();
            long createTimeTime = createTime.getTime();

            if (commodityOrder.getOrderType() == 1) {
                createTimeTime += commoditySetting.getFlashOvertime() * 60 * 1000;
            }else if (commodityOrder.getOrderType() == 2){
                createTimeTime += commoditySetting.getNormalOvertime() * 60 * 1000;
            }
            Date timeout = new Date(createTimeTime);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(sdf.format(timeout));
            commodityOrder.setTimeout(timeout);
        }
        return sysCommodityOrders;
    }

    /**
     * 新增订单管理
     *
     * @param sysCommodityOrder 订单管理
     * @return 结果
     */
    @Override
    public int insertSysCommodityOrder(SysCommodityOrder sysCommodityOrder) {
        sysCommodityOrder.setCreateTime(DateUtils.getNowDate());
        return sysCommodityOrderMapper.insertSysCommodityOrder(sysCommodityOrder);
    }

    /**
     * 修改订单管理
     *
     * @param sysCommodityOrder 订单管理
     * @return 结果
     */
    @Override
    public int updateSysCommodityOrder(SysCommodityOrder sysCommodityOrder) {
        return sysCommodityOrderMapper.updateSysCommodityOrder(sysCommodityOrder);
    }

    /**
     * 批量删除订单管理
     *
     * @param ids 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteSysCommodityOrderByIds(Long[] ids) {
        return sysCommodityOrderMapper.deleteSysCommodityOrderByIds(ids);
    }

    /**
     * 删除订单管理信息
     *
     * @param id 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteSysCommodityOrderById(Long id) {
        return sysCommodityOrderMapper.deleteSysCommodityOrderById(id);
    }
}
